import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import db.DatabaseConnector;

public class ShowSCategories extends HttpServlet {
    
//doPost method included for POST requests
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        doGet(request, response);
    }
    
//Show the options for Selected Search Categories; called by SearchLibrary.jsp
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String tablename = request.getParameter("tName");
        
        //Variables for the database connection
        DatabaseConnector con = new DatabaseConnector(DatabaseConnector.getDatabaseType());
        DatabaseConnector con3 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        try {
            int lopt = 0;
            String lstr = "";

            String SQL = "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME in ('"+tablename+"')";
            String[][] col = con3.selectSQL(SQL,1);
            SQL = "SELECT * FROM " + tablename + " WHERE Active = ?";
            String[][] rs = con.selectSQL(SQL,col.length,new Object[]{1});
            
            String defaultSel = "Doesn`t Matter";
            for (int i=0;i<rs.length;i++){
                String rsCat = rs[i][1];
                String rsPurpose = rs[i][2];
                String rsType = rs[i][3];
                String rsNumber = rs[i][5];

                out.print("<tr>");
                out.print("<td valign=\"top\" bgcolor=\"eeeeff\"><b>" + rsCat.replace('_', ' ') + ":</b></td>");
                //out.print("<td valign=\"top\">(" + rsPurpose + ")</td>");
                out.print("<td>");
                
                if(rsType.equals("text")) {
                    out.print("<input type=\"text\" size=\"40\" name=\"RD" + rsCat);
                    out.print("\" value=\"\"></td>");
                } else if(rsType.equals("option")) {
                    if(rsNumber.equals("2")) {
                        String lstr1 = rs[i][findInArray(col,"t0")];
                        String lstr2 = rs[i][findInArray(col,"t1")];
                        out.print("<INPUT TYPE ='radio' NAME = 'RD" + rsCat);
                        out.println("' VALUE='" + lstr1 + "'>" + lstr1);
                        out.print("&nbsp;<INPUT TYPE ='radio' NAME = 'RD" + rsCat);
                        out.println("' VALUE='" + lstr2 + "'>" + lstr2);
                        out.print("&nbsp;<INPUT TYPE ='radio' NAME = 'RD" + rsCat);
                        out.println("' VALUE='Show All' CHECKED>" + defaultSel);
                        out.println("</td>");
                    } else {
                        out.print("<SELECT NAME = 'RD" + rsCat);
                        out.print("' SIZE = '1'>");
                        out.println("<option value=\"Show All\">" + defaultSel + "</option>");
                        for(lopt = 0; lopt < (Integer.valueOf(rsNumber).intValue()); lopt++) {
                            lstr = rs[i][findInArray(col,"t"+lopt)];
                            out.println("<option value=\"" + lstr + "\">" + lstr + "</option>");
                        }
                        out.println("</select></td>");
                    }
                } else if(rsType.equals("set")) {
                    for(lopt = 0; lopt < (Integer.valueOf(rsNumber).intValue()); lopt++) {
                        lstr = rs[i][findInArray(col,"t"+lopt)];
                    	out.print("<input name=\"RD" + rsCat + "\"");
                    	out.println("type=\"checkbox\" value=\"" + lstr + "\">" + lstr + "<br>");
                    }
                    out.println("</td>");
                }
                out.println("</tr>");
            }
        }
        finally
        {
            con.CleanUp();
            con3.CleanUp();
        }
    }

    private int findInArray(String[][] ar, String str){
        for (int i=0;i<ar.length;i++){
            if (ar[i][0].equals(str)) return i;
        } return -1;
    }
}
